﻿@{ Layout = "~/Views/Shared/_ModalForm.cshtml";
}
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment
@section header{
    <link href="@Url.Content("~/lib/summernote/0.8.15/summernote.min.css")" rel="stylesheet" type="text/css">
    <script src='@Url.Content("~/lib/summernote/0.8.15/summernote.js")' type="text/javascript"></script>
    <script src='@Url.Content("~/lib/summernote/0.8.15/lang/summernote-zh-CN.min.js")' type="text/javascript"></script>
}

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <div class="col-sm-12">
                <div id="body" class="summernote"></div>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var act = 'editBody';
    var index = '@ViewBag.Index';
    var obj = '@ViewBag.Object';
    var objId = '@ViewBag.ObjectId';
    var newFileTitle = '@ViewBag.NewFileTitle';
    var uploadType = '@ViewBag.UploadType';

    $(function () {
        $('#body').summernote({
            height: '520px',
            lang: 'zh-CN',
            dialogsInBody: true,
            callbacks: {
                onImageUpload: function (files, editor, welEditable) {
                    insertBodyImage(files[0], editor, welEditable);
                }
            }
        });

        getForm();
        $("#form").validate({
            rules: {
            }
        });
    });

    function getForm() {
            ys.ajax({
                url: '@Url.Content("~/Scc/Article/GetEditBodyDtoJson")' + '?id=' + id,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        //$("#form").setWebControls(rst.Data);
                        $("#body").summernote('code', rst.Data.Body);
                    }
                    else ys.msgError(rst.Message);
                }
            });
    }

    function saveForm(index) {
        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            postData.Body = $("#body").summernote('code');
            ys.ajax({
                url: '@Url.Content("~/Scc/Article/")' + act,
                type: "post",
                data: postData,
                success: function (rst) {
                    if (rst.Flag == 1) {
                        ys.msgSuccess(rst.Message);
                    }
                    else ys.msgError(rst.Message);
                }
            });
        }
    }

    function insertBodyImage(file, editor, welEditable) {
        var formdata = new FormData(); 
        formdata.append("fileList", file);
        ys.ajaxUploadFile({
            url: '@Url.Content("~/File/Upload")' + '?index=' + index + '&obj=' + obj + '&objId=' + objId + '&newFileTitle=' + newFileTitle + '&uploadType=' + uploadType ,
            data: formdata,
            success: function (rst) {
                if (rst.Flag == 1) {
                    $("#body").summernote('insertImage', '@Url.Content("~/File/GetImage")' + '?index=' + index + '&obj=' + obj + '&objId=' + objId + '&fileName=' + (rst.Data) , '/');
                }
                else ys.msgError(rst.Message);
            }
        })
    }



</script>
